import { ACTOR_TYPE, BActor } from "../"

import vtkActor from "@kitware/vtk.js/Rendering/Core/Actor"
import vtkMapper from "@kitware/vtk.js/Rendering/Core/Mapper"
import vtkPolyData from "@kitware/vtk.js/Common/DataModel/PolyData"
import vtkPoints from "@kitware/vtk.js/Common/Core/Points"
import vtkCellArray from "@kitware/vtk.js/Common/Core/CellArray"
import { Vector3 } from "@kitware/vtk.js/types"

import { LineOptions, MeshEdgeData } from "../interface"

export class LineActor extends BActor {
    readonly type = ACTOR_TYPE.GRAPHIC_ACTOR
    createdBy = "default"
    instance: vtkActor = vtkActor.newInstance()
    mapper: vtkMapper = vtkMapper.newInstance()
    polyData: vtkPolyData = vtkPolyData.newInstance()
    points: vtkPoints = vtkPoints.newInstance()
    cells: vtkCellArray = vtkCellArray.newInstance()
    start: Vector3 = [0, 0, 0]
    end: Vector3 = [0, 0, 0]
    meshEdgeData: MeshEdgeData | null = null
    private _config: LineOptions = {}

    constructor(
        p1: Vector3,
        p2: Vector3,
        meshEdgeData: MeshEdgeData = { uid: "" },
        options: LineOptions = {}
    ) {
        super()
        console.log("meshEdgeData", meshEdgeData)
        this.start = p1
        this.end = p2
        this._config = options
        this.meshEdgeData = meshEdgeData || { uid: "" }
        this.createPoints()
        this.polyData.setPoints(this.points)
        this.createLine()
        this.polyData.setLines(this.cells)
        this.mapper.setInputData(this.polyData)
        this.initOptions()
        this.instance.setMapper(this.mapper)
    }

    initOptions() {
        if (this._config.color) {
            this.instance.getProperty().setColor(...this._config.color)
        } else {
            this.instance.getProperty().setColor(0, 0, 1)
        }
        if (this._config.lineWidth) {
            this.instance.getProperty().setLineWidth(this._config.lineWidth)
        } else {
            this.instance.getProperty().setLineWidth(5)
        }
    }

    createPoints() {
        this.points.setData(
            new Float32Array([
                this.start[0],
                this.start[1],
                this.start[2],
                this.end[0],
                this.end[1],
                this.end[2],
            ]),
            3
        )
    }

    createLine() {
        this.cells.insertNextCell([0, 1])
    }
}
